/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/dao/expression/Equal.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: Equal.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (周一, 02 七月 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.dao.expression;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 * @author elancom
 * @date Apr 13, 2012
 */
public class Equal implements Condition {
	private static final long serialVersionUID = 6401468361605820992L;

	private final String propertyName;

	private final Object value;

	private final boolean isTargetProperty;

	public Equal(String propertyName, Object value) {
		this(propertyName, value, false);
	}

	public Equal(String propertyName, Object value, boolean isTargetProperty) {
		super();
		this.propertyName = propertyName;
		this.value = value;
		this.isTargetProperty = isTargetProperty;
	}

	@Override
	public Criterion toCriterion() {
		return !isTargetProperty ? Restrictions.eq(propertyName, value) : Restrictions.eqProperty(propertyName, value.toString());
	}

	@Override
	public String toHQLString() {
		final StringBuilder sb = new StringBuilder();
		sb.append(propertyName).append(" = ").append(TypeUtils.toValue(value));
		return sb.toString();
	}

}
